package cn.com.yang.datastruct.stack;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * @since 2023.07.18
 * 增强版的栈
 */
public class HighStack {
    private Object[] elemData;
    private int maxSize;
    private int top;

    public HighStack(){
        elemData = new Object[10];
        this.maxSize =10;
        top = -1;
    }
    // 压栈
    public void push(Object data){
        if (top==maxSize-1){
            if (maxSize - Integer.MAX_VALUE > 0){
                maxSize = Integer.MAX_VALUE;
            }else {
                maxSize = maxSize << 1;
            }
            elemData = Arrays.copyOf(elemData,maxSize);
        }
        elemData[++top]=data;
    }
    // 查询顶栈
    public Object peek(){
        if (top==-1){
            throw new EmptyStackException();
        }
        return elemData[top];
    }
    // 出栈
    public Object pop(){
        Object obj = peek();
        elemData[top] = null;
        this.top--;
        return obj;
    }
    // 是否是空栈
    public boolean isEmpty(){
        return (top==-1);
    }
    //是否满栈
    public boolean isFull(){
        return top == maxSize-1;
    }
}
